/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhnb
 * Date: 2011-4-11 下午05:07:30
 */

package com.ccx.dbpool;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jolbox.bonecp.BoneCP;

/**
 * BoneCP池集类
 * @author zhnb
 *
 */
public class BoneCPs implements Serializable {
	
	private static final long serialVersionUID = 5245114645894959146L;
	private static Logger logger = LoggerFactory.getLogger(BoneCPs.class);
	
	//数据池集合<key_dataSourceName, value_connectionPool>
	public static Map<String, BoneCP> poolsMap = null;
	
	//加载时实例化池集
	static {
		try {
			BoneCPsUtil bcpsUtil = new BoneCPsUtil();
			poolsMap = bcpsUtil.getPoolsMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 从指定数据源获取一条连接
	 * @param dataSourceName		数据源名称
	 * @return						数据库连接
	 */
	public static Connection getConnection( String dataSourceName ){
		
		Connection conn = null;
		
		BoneCP boneCP = BoneCPs.poolsMap.get( dataSourceName );
		try {
			conn = boneCP.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return conn;
	}

}
